<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Introduce_model extends MY_Model {

    protected $introduce;

    function __construct(){
        parent::__construct();
    }

    function table_name(){
        return "introduce";
    }

    function get_introduces($filters = array(),$counting = false){
        if (isset($this->CI->cache)){
            $caching = true;
            $cache_key = 'ci--INTRODUCE_'.md5(serialize($filters));
            if ($counting){
                $cache_key = 'ci--INTRODUCE_COUNT_'.md5(serialize($filters));
            }
            if ($return = $this->cache->file->get($cache_key)) {
                return ($return == 'empty_cache') ? FALSE : $return;
            }
        }else{
            $caching = false;
        }

        $sql = "SELECT * FROM introduce WHERE 1=1 ";
        $param = array();
        if (isset($filters['introduce_id'])){
            $sql .= ' AND introduce_id = ?';
            $param[] = $filters['introduce_id'];
        }

        if (isset($filters['name'])){
            $sql .= " AND name like ?";
            $param[] = '%' .$filters['name'] . '%';
        }

        if (isset($filters['status'])){
            $sql .= ' AND status = ?';
            $param[] = $filters['status'];
        }

        if (isset($filters['alias'])){
            $sql .= ' AND alias = ?';
            $param[] = $filters['alias'];
        }

        if (isset($filters['other_introduce_id'])){
            $sql .= ' AND introduce_id <> ?';
            $param[] = $filters['other_introduce_id'];
        }

        $sql .= " ORDER BY introduce_id DESC";
        if (isset($filters['limit'])) {
            $offset = (isset($filters['page'])) ? $filters['page'] : 0;
            $sql .= ' LIMIT ?, ?';
            $param[] = (int)$offset;
            $param[] = (int)$filters['limit'];
        }

        $result = $this->db->query($sql, $param);

        if($counting !=false){
            $rows = $result->num_rows();
            $result->free_result();

            if ($caching == TRUE) {
                $this->CI->cache->file->save($cache_key, $rows, (5*60));
            }

            return $rows;
        }
        if ($result->num_rows() == 0) {
            return false;
        }
        $introduce = array();
        foreach ($result->result_array() as $row ){
            $row['link'] = create_url('gioi-thieu/'.$row['alias'],array('rewrite'=>true));
            $row['date'] = fdate($row['created'],"%A, %d/%m/%Y - %H:%M");
            $introduce[] = $row;
        }
        if ($caching){
            $this->CI->cache->file->save($cache_key,$introduce);
        }
        $result->free_result();

        return $introduce;
    }

    function get_count($filter = array()){
        return $this->get_introduces($filter,true);
    }

    function get_introduce($introduceId){
        if (!isset($this->introduce[$introduceId])){
            $introduce = $this->get_introduces(array('introduce_id'=>$introduceId));
            $this->introduce[$introduceId] = $introduce;
        }else{
            $introduce = $this->introduce[$introduceId];
        }
        if (empty($introduce)) {
            return false;
        }
        return $introduce[0];
    }

    function insert_introduce($data){
        $_introduce_id = $this->save($data);
        return $_introduce_id;
    }

    function update_introduce($data,$introduce_id){
        $attributes = $this->delete_image_file($introduce_id, $data);
        return $this->update($attributes, array('introduce_id' => $introduce_id));
    }

    function delete_image_file($introduce_id, $data = false, $is_delete = false){
        $files = $this->db->select('image')->from('introduce')->where('introduce_id',$introduce_id)->get()->result_array();
        foreach ($files[0] as $k=>$v){
            if (is_array($data) && count($data) > 0 && array_key_exists($k.'_delete',$data)){
                if (file_exists($v)){
                    unlink($v);
                }
                $data[$k]="";
            }else if ($is_delete){
                if (file_exists($v)){
                    unlink($v);
                }
            }
        }
        return $data;
    }

    function delete_introduce($introduce_id) {
        $this->delete_image_file($introduce_id);
        return $this->delete(array('introduce_id'=>$introduce_id));
    }

    function change_status_introduce($introduce_id) {
        $status = $this->db->select('status')->from('introduce')->where('introduce_id', $introduce_id)->get()->result_array();

        if ($status[0]['status'] == 1) {
            $change = 0;
        } else {
            $change = 1;
        }
        return $this->update(array('status' => $change), array('introduce_id' => $introduce_id));
    }

    function get_introduce_by_alias($introduce_alias){
        $introduce = $this->get_introduces(array('alias'=>$introduce_alias),false);
        if ($introduce){
            return $introduce[0];
        }
        return false;
    }

}
?>